using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine.Windows;

public class WebRequest : MonoBehaviour
{
    private void Start()
    {
        GetBookSprite("https://test.cuihumatou.com/cup.png", this.transform);
    }

    public void GetBookSprite(string url, Transform transform)
    {
        StartCoroutine(DownSprite(url, transform));
        //var wwww = new WWW("https://test.cuihumatou.com/cup.png");
    }

    IEnumerator DownSprite(string url, Transform transform)
    {
        using (UnityWebRequest request = new UnityWebRequest(url))
        {
            DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
            request.downloadHandler = texDl;
            yield return request.SendWebRequest();
            if (request.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.LogError(request.error);
            }
            else
            {
                Texture2D tex = texDl.texture;
                Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
                transform.GetComponent<Image>().sprite = sprite;
            }
        }
    }

    IEnumerator Post(string url, Dictionary<string, string> postData)
    {
        UnityWebRequest request = new UnityWebRequest(url, "POST");
        request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        string data = "";
        foreach (KeyValuePair<string, string> pair in postData)
        {
            data += string.Format("{0}={1}&", pair.Key, pair.Value);
        }
        byte[] bytes = Encoding.UTF8.GetBytes(data);
        request.uploadHandler = new UploadHandlerRaw(bytes);
        request.downloadHandler = new DownloadHandlerBuffer();
        yield return request.SendWebRequest();
        if (request.result == UnityWebRequest.Result.Success)
        {
            string response = request.downloadHandler.text;
            Debug.Log(response);
        }
        else
        {
            Debug.LogError(request.error);
        }
    }


    IEnumerator Get(string url)
    {
        using (UnityWebRequest request = UnityWebRequest.Get(url))
        {
            yield return request.SendWebRequest();
            if (request.result == UnityWebRequest.Result.Success)
            {
                string response = request.downloadHandler.text;
                Debug.Log(response);
            }
            else
            {
                Debug.LogError(request.error);
            }
        }
    }

    IEnumerator UploadFile(string url, string filePath)
    {       
        byte[] bytes = File.ReadAllBytes(filePath);
        UnityWebRequest request = UnityWebRequest.PostWwwForm(url, "POST");
        request.uploadHandler = new UploadHandlerRaw(bytes);
        request.downloadHandler = new DownloadHandlerBuffer();
        yield return request.SendWebRequest();
        if (request.result == UnityWebRequest.Result.Success)
        {
            string response = request.downloadHandler.text;
            Debug.Log(response);
        }
        else
        {
            Debug.LogError(request.error);
        }
    }
}